// 搜索栏
import 'package:flutter/material.dart';

class RadioSelect extends StatefulWidget {
  final String title;
  final String defaultvalue;
  final List<Map> items;
  final void Function(Map value) callback;
  const RadioSelect(
      {Key key, this.title, this.defaultvalue, this.items, this.callback})
      : super(key: key);

  @override
  _RadioSelectState createState() => _RadioSelectState();
}

class _RadioSelectState extends State<RadioSelect> {
  String value;

  @override
  void initState() {
    super.initState();
    print(widget.defaultvalue);
    value = widget.defaultvalue ?? widget.items[0]['value'];
  }

  @override
  dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          height: 40,
          padding: EdgeInsets.only(left: 16),
          alignment: Alignment.centerLeft,
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 0.5,
                color: Color.fromRGBO(229, 229, 229, 1),
              ),
            ),
          ),
          child: Text(
            widget.title,
            style: TextStyle(
              fontSize: 14,
              height: 20 / 14,
              color: Color.fromRGBO(150, 151, 154, 1),
            ),
          ),
        ),
        Column(
          children: getItemList(widget.items),
        ),
      ],
    );
  }

  List<Widget> getItemList(List items) {
    return items.map<Widget>((item) => createItem(item)).toList();
  }

  Widget createItem(item) {
    return GestureDetector(
      onTap: () {
        setState(() {
          value = item['value'];
        });
        widget.callback(item);
      },
      child: Container(
        height: 62,
        width: double.infinity,
        alignment: Alignment.center,
        padding: EdgeInsets.only(left: 16, right: 16),
        decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          border: Border(
            bottom: BorderSide(
              width: 0.5,
              color: Color.fromRGBO(229, 229, 229, 1),
            ),
          ),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Container(
                child: Text(
                  item['lable'],
                  style: TextStyle(
                    fontSize: 16,
                    height: 19 / 16,
                  ),
                ),
              ),
            ),
            item['value'] == value
                ? Image(
                    image: AssetImage('assets/images/radio.png'),
                    width: 15,
                    height: 11,
                  )
                : SizedBox(),
          ],
        ),
      ),
    );
  }
}
